package cn.mastor.pet.controller;

import cn.mastor.basic.config.BussinessException;
import cn.mastor.basic.util.LoginContext;
import cn.mastor.org.domain.Employee;
import cn.mastor.org.mapper.EmployyMapper;
import cn.mastor.pet.domain.SearchMasterMsgAuditLog;
import cn.mastor.pet.dto.AcceptDto;
import cn.mastor.pet.query.SearchMasterMsgAuditLogQuery;
import cn.mastor.pet.service.ISearchMasterMsgService;
import cn.mastor.pet.domain.SearchMasterMsg;
import cn.mastor.pet.query.SearchMasterMsgQuery;
import cn.mastor.basic.util.AjaxResult;
import cn.mastor.basic.util.PageList;
import cn.mastor.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    EmployyMapper employyMapper;


    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.delete(id);

        } catch (Exception e) {
        e.printStackTrace();
        }
        return AjaxResult.me().success();
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.findById(id);
    }
    @GetMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable("id")Long id,HttpServletRequest request)
    {
        return searchMasterMsgService.reject(id,request);
    }

    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryByPage(query);
    }
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg)
    {
        searchMasterMsgService.publish(searchMasterMsg);
        return AjaxResult.me().success();
    }
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request)
    {
        return searchMasterMsgService.querySearchMasterMsg(query);
    }
    //toSearchMasterPool
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> toSearchMasterPool(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request)
    {
        if (null==LoginContext.getLogininfo(request)){
            throw new BussinessException("登录失效，请重新登录");
        }
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employyMapper.findBylogininfoId(logininfo.getId());
        Long shopId = employee.getShopId();
        query.setShopId(shopId);
        query.setState(2);
        return searchMasterMsgService.querySearchMasterMsg(query);
    }
    @PostMapping("/toSearchMasterPool")
    public PageList<SearchMasterMsg> toSearchMasterfin(@RequestBody SearchMasterMsgQuery query)
    {
        query.setShopId(null);
        query.setState(1);
        return searchMasterMsgService.querySearchMasterMsg(query);
    }
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request)
    {
        if (null==LoginContext.getLogininfo(request)){
            throw new BussinessException("登录失效，请重新登录");
        }
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employyMapper.findBylogininfoId(logininfo.getId());
        Long shopId = employee.getShopId();
        query.setShopId(shopId);
        query.setState(1);
        return searchMasterMsgService.querySearchMasterMsg(query);
    }
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody AcceptDto acceptDto)
    {
        return searchMasterMsgService.accept(acceptDto);
    }
}
